// pages/fuli/houbao/houbao.js
var app = getApp();
var Crypto = require('../../../utils/cryptojs/cryptojs.js').Crypto;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Money:['50','100','300','500'],
    currentMoney:50,
    userInfo:[],
    tempFilePaths:'',
    guanggaoPic:'',
    balance:[],

    count:'',
    kouling:'',
    advertName:'',                //品牌
    advertLink: "",               // 链接
    advertDec: "",                  // 描述
    ossLogoUrl: "",
    ossImageUrl: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      pingpai: '',
      lianjie: '',
      count: '',
      kouling: '',
    })
    wx.getUserInfo({
      success:function(res){
        console.log(res)
        that.setData({
          userInfo:res.userInfo,
          ossLogoUrl: res.userInfo.avatarUrl
        })
      },
    }) 
    that.balance();
    that.advertInfo()   
  },
  // 上次发红包的信息
  advertInfo:function(){
    var that= this;
    console.log("sdjlkdjlkasjdlkfjlsdkjflksdjlfjsdlkjflksdjflksdjfkls ")
    wx.getStorage({
      key: 'token',
      success: function(res) {
        var token = res.data;
        var timestamp = app.timestamp();
        var url = app.globalData.localApi+"/advert/redpackInfo";
        wx.request({
          url: url,
          method:'POST',
          data:{
            token:token,
            timestamp:timestamp,
            sign:Crypto.MD5(app.sign(url,token,timestamp)),
            url:app.urlStr(url),
          },
          success:function(res){
            console.log(res)
            if(res.data.code==1){
              console.log("请求成功，但是没有数据")
            }else if(res.data.code==0){
              that.setData({
                count: '',
                kouling: '',
                advertName: res.data.data.advertName,                //品牌
                advertLink: res.data.data.advertLink,               // 链接
                advertDec: res.data.data.advertDec,                  // 描述
                ossLogoUrl: res.data.data.logo,
                ossImageUrl: res.data.data.ptimage,
              })
            }
          }
        })
      },
    })
  },
  setMoney:function(e){
    var that = this;
    that.setData({
      currentMoney:e.currentTarget.dataset.data,
    })
  },
  clearInput:function(){
    var that = this;
    that.setData({
      currentMoney:''
    })
  },
  // 上传logo
  uploadLogo:function(){
    var that = this;
    console.log("调用logo")
    wx.chooseImage({
      count: 1, // 默认9
      success: function (res) {
        var tempFilePaths = res.tempFilePaths.toString();
        that.setData({
          tempFilePaths: tempFilePaths,
        })
        that.uploadImg(that.data.tempFilePaths, 1);
      }
    })
  },
  // 点击选取图片；
  ChooseImg:function(){
    var that = this;
    wx.chooseImage({
      count:1,
      success: function(res) {
        console.log(res)
        that.setData({
          ossImageUrl:res.tempFilePaths.toString(),
        })
        that.uploadImg(that.data.ossImageUrl,2);
      },
    })
  },
  // 上传图片；
  uploadImg:function(filePath,logo){
    var that = this; 
    wx.getStorage({
      key: 'token',
      success: function(res) {
        console.log(res.data);
        var token = res.data;
        var url = app.globalData.localApi +"/advert/images";
        var timestamp = app.timestamp();
        // var len = filePath.length;
        // for(var i = 0;i<len;i++){
          wx.uploadFile({
            url: url,
            filePath: filePath,
            name: 'image',
            formData:{
              token:token,
              timestamp: timestamp,
              sign:Crypto.MD5(app.sign(url,token,timestamp)),
              url:app.urlStr(url),
              islogo:logo
            },
            success:function(res){
              var result= JSON.parse(res.data)
              console.log(result)
              console.log("上传成功:     "+logo)
              console.log(res)
              if(logo==1){
                that.setData({
                  ossLogoUrl:result.ossLogoUrl
                })
              }else if(logo==2){
                that.setData({
                  ossImageUrl: result.ossImageUrl
                })
              }
            },
            fail:function(res){
              console.log("上传失败")
              console.log(res)
            }
          })
        // }

        // wx.request({
        //   url: url,
        //   method:"POST",
        //   data:{
        //     token:token,
        //     timestamp:timestamp,
        //     sign:Crypto.MD5(app.sign(url,token,timestamp)),
        //     url:app.urlStr(url),

        //   }
        // })
      },
    })

  },
  // 生成广告红包
  toshopShare:function(){
    var that = this;
    if (that.data.ossLogoUrl==''){
      app.showInfo("品牌logo丢失，请重新上传")
    } else if (that.data.advertName==''){
      app.showInfo("请输入品牌名称")
    } else if (that.data.kouling==''){
      app.showInfo("请输入语音口令")
    }else if(that.data.count==''){
      app.showInfo("请输入红包个数")
    } else if (that.data.advertDec==''){
      app.showInfo("请输入广告描述")
    } else{
      that.shopPrepay();
    }
    // that.shopPrepay();
    
  },
  // 获取数据
  getPingpai:function(e){
    var that = this;
    var pingpai = e.detail.value;
    console.log(pingpai)
    that.setData({
      advertName:pingpai,
    })
  },
  getLianjie:function(e){
    var that = this;
    var lianjie = e.detail.value;
    console.log(lianjie);
    that.setData({
      advertLink:lianjie
    })
  },
  getKouling: function (e) {
    var that = this;
    var kouling = e.detail.value;
      that.setData({
        kouling: kouling
      })
    console.log(kouling)
  },
  getKouling1: function (e) {
    var that = this;
    var kouling = e.detail.value;
    var isChina = app.checkChinese(kouling)
    if (isChina) {
      that.setData({
        kouling: kouling
      })
    } else {
      app.showInfo("请输入中文口令")
    }
    console.log(kouling)

  },
  getCount: function (e) {
    var that = this;
    var count = e.detail.value;
    console.log(count)
    that.setData({
      count: count
    })
  },
  getTextArea:function(e){
    var that = this;
    var advertDec = e.detail.value;
    console.log(advertDec)
    that.setData({
      advertDec: advertDec
    })
  },
  // 生成广告红包
  shopPrepay: function () {
    console.log("执行prepay")
    var that = this;
    console.log(that.data.formId)
    wx.getStorage({
      key: 'token',
      success: function (res) {
        var url = app.globalData.localApi + "/redpack/prepay";
        var token = res.data;
        var timestamp = app.timestamp()
        console.log(that.data.houbaoStyle)
        wx.request({
          url: url,
          method: "POST",
          data: {
            timestamp: timestamp,
            token: token,
            url: app.urlStr(url),
            sign: Crypto.MD5(app.sign(url, token, timestamp)),
            advertName: that.data.advertName,
            packText: that.data.kouling,
            packMoney: that.data.currentMoney * 100,
            packCount: parseInt(that.data.count),
            packAnswer: that.data.answer,
            packType:4,
            formId: that.data.formId,
            ossMessage: that.data.qingtingMpVoice,
            advertLink: that.data.advertLink,               // 链接
            advertDec: that.data.advertDec,                  // 描述
            ossLogoUrl: that.data.ossLogoUrl,
            ossImageUrl:that.data.ossImageUrl,
          },
          success: function (res) {
            console.log("prepay执行成功")
            console.log(res)
            that.setData({
              clickHot: true,
            })
            if (res.data.code == 201) {
              app.login();
            }
            if (res.data.error !== undefined) {
              app.showInfo(res.data.error);
            } else if (res.data.data.payinfo == '' || res.data.data.payinfo == undefined) {
              that.setData({
                orderno: res.data.data.orderinfo.orderno,
                tempFilePathVoice: res.data.data.orderinfo.ossMp3Url,
              })
              console.log("返回语音接口地址")
              console.log(that.data.tempFilePathVoice)
              wx.navigateTo({
                url: '../shopShare/shopShare?houBaoStyle=' + that.data.houBaoStyle
                + "&mpUrl=" + that.data.tempFilePathVoice
                + "&Money=" + that.data.Money
                + "&Number=" + that.data.Number
                + "&kouling=" + that.data.kouling
                + "&orderno=" + that.data.orderno
                + "&answer=" + that.data.answer,
              })
            } else {
              // d微信支付；
              that.setData({
                orderno: res.data.data.orderinfo.orderno
              })
              wx.requestPayment({
                'timeStamp': res.data.data.payinfo.timeStamp.toString(),
                'nonceStr': res.data.data.payinfo.nonceStr,
                'package': res.data.data.payinfo.package,
                'signType': 'MD5',
                'paySign': res.data.data.payinfo.paySign,
                'success': function (res) {
                  console.log("支付结果")
                  console.log(res)
                  wx.navigateTo({
                    url: '../shopShare/shopShare?houBaoStyle=' + that.data.houBaoStyle
                    + "&mpUrl=" + that.data.tempFilePathVoice
                    + "&Money=" + that.data.Money
                    + "&Number=" + that.data.Number
                    + "&kouling=" + that.data.kouling
                    + "&orderno=" + that.data.orderno
                    + "&answer=" + that.data.answer,
                  })
                },
                'fail': function (res) {
                  console.log("支付结果")
                  console.log(res)
                }
              })
            }
          }
        })
      },
    })
  },
  // 请求余额
  balance: function () {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        var token = res.data;
        var timestamp = app.timestamp();
        var url = app.globalData.localApi + "/user/balance";
        var data = {
          timestamp: timestamp,
          token: token,
          sign: Crypto.MD5(app.sign(url, token, timestamp)),
          url: app.urlStr(url),
        };
        wx.request({
          url: url,
          method: "POST",
          data: data,
          success: function (res) {
            console.log(res.data)
            if (res.data.code == 201) {
              app.login();
            }
            that.setData({
              balance: res.data.data,
            })
          },
        })
      },
    })
  },
  tomyRecord:function(){
    var that = this;
    wx.navigateTo({
      url: '../../index/mRecord/myRecord',
    })
  },
  toQuestion:function(){
    var that = this;
    wx.navigateTo({
      url: '../../question/question',
    })
  },
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that= this;
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})